package com.cn.daifaming.demoservice.service;

import java.util.HashMap;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cn.daifaming.demoservice.common.utils.CustomizedPropertyConfigurer;
import com.cn.daifaming.demoservice.common.utils.DateTimeUtils;
import com.cn.daifaming.demoservice.common.utils.EmailSendUtil;
import com.cn.daifaming.demoservice.common.utils.EncryptUtil;
import com.cn.daifaming.demoservice.common.utils.IdGenerator;
import com.cn.daifaming.demoservice.dao.UserInfoDao;
import com.cn.daifaming.demoservice.domain.SignupReq;
import com.cn.daifaming.demoservice.pojo.User;

/**
 * @author Administrator
 *
 */
@Service
public class UserInfoService
{
	private static final Logger LOG = LoggerFactory
			.getLogger(UserInfoService.class);

	@Autowired
	private UserInfoDao userInfoDao;

	public void insert(SignupReq signupReq)
	{
		User user = new User();
		BeanUtils.copyProperties(signupReq, user);
		user.setId(IdGenerator.generate());
		user.setUserId(UUID.randomUUID().toString());
		user.setCreateTime(DateTimeUtils.getCurrentStringTime());
		user.setPasswd(EncryptUtil.encoderByMd5(user.getPasswd()));

		LOG.info("the user info is:{}", user);
		userInfoDao.insert(user);

		HashMap<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put("name", user.getUserName());
		EmailSendUtil.sendHtmlMail(user.getEmail(),
				CustomizedPropertyConfigurer.getContextProperty("subject"),
				CustomizedPropertyConfigurer
						.getContextProperty("signup_template"),
				jsonMap);
	}
}
